package org.jeecg.modules.yw.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yw.entity.AttendanceRecord;
import org.jeecg.modules.yw.service.AttendanceRecordService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "考勤记录")
@RequestMapping("/attendanceRecord")
public class AttendanceRecordController extends JeecgController<AttendanceRecord, AttendanceRecordService> {
    @Autowired
    private AttendanceRecordService attendanceRecordService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @ApiOperation(value = "获取考勤记录")
    @GetMapping("/page")
    public  org.jeecg.common.api.vo.Result page(
                       @RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "mobile", required = false) String mobile,
                       @RequestParam(value = "attendmonth", required = false) String attendmonth,
                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize ) {
        Page<AttendanceRecord> page=new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        QueryWrapper<AttendanceRecord> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(mobile)) {
            queryWrapper.eq("mobile", mobile);
        }
        if (!StringUtils.isEmpty(attendmonth)) {
            queryWrapper.eq("attendmonth", attendmonth);
        }
        queryWrapper.orderByDesc("name");
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(!StringUtils.isEmpty(area)){
                String[] areas=area.split(",");
                StringBuffer st=new StringBuffer();
                for (int i = 0; i < areas.length; i++) {
                    st.append("\""+areas[i]+"\",");
                }
                st=st.deleteCharAt(st.length()-1);
                String sql="(select id from enterprise where city_code in ("+st.toString()+") " +
                        "or (province_code in  ("+st.toString()+") and city_code is null))";
                queryWrapper.apply("userid in"+sql+"");
            }else{
                IPage<AttendanceRecord> result =new Page<>();
                return org.jeecg.common.api.vo.Result.ok(result);
            }
        }
        IPage<AttendanceRecord> result = attendanceRecordService.page(page, queryWrapper);
        return  org.jeecg.common.api.vo.Result.ok(result);
    }
    @ApiOperation(value = "导出考勤记录")
    @GetMapping("/export")
    public ModelAndView export(HttpServletRequest request,
                               @RequestParam(value = "name", required = false) String name,
                               @RequestParam(value = "mobile", required = false) String mobile,
                               @RequestParam(value = "attendmonth", required = false) String attendmonth,
                               @RequestParam(value = "ids", required = false) String ids ) {
        QueryWrapper<AttendanceRecord> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(mobile)) {
            queryWrapper.eq("mobile", mobile);
        }
        if (!StringUtils.isEmpty(attendmonth)) {
            queryWrapper.eq("attendmonth", attendmonth);
        }
        if (!StringUtils.isEmpty(ids)){
            String[] names = ids.split(",");
            for (String id : names) {
                String[] str = id.split("@@");
                queryWrapper.or(qw->qw.eq("userId",str[0]).eq("attendmonth",str[1]));
            }
        }
        queryWrapper.orderByDesc("name");
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(!StringUtils.isEmpty(area)){
                String[] areas=area.split(",");
                StringBuffer st=new StringBuffer();
                for (int i = 0; i < areas.length; i++) {
                    st.append("\""+areas[i]+"\",");
                }
                st=st.deleteCharAt(st.length()-1);
                String sql="(select id from enterprise where city_code in ("+st.toString()+") " +
                        "or (province_code in  ("+st.toString()+") and city_code is null))";
                queryWrapper.apply("userid in"+sql+"");
            }else{
                String user= UserUtil.getUserName(request);
                return super.exportXls(new ArrayList<>(), AttendanceRecord.class,user, "考勤审批");
            }
        }
        List<AttendanceRecord> result = attendanceRecordService.list( queryWrapper);
        String user= UserUtil.getUserName(request);
        return super.exportXls(result, AttendanceRecord.class,user, "考勤审批");
    }
}
